using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace stsdev
{

    public class CabDdfBuilder
    {
        public static void Create(string FilePath)
        {
            //FileAttributes at;
            Create(FilePath, @"bin/debug/");

            File.SetAttributes(FilePath, FileAttributes.Normal);
        }
        public static void Create(string FilePath, string outdir)
        {
            StreamWriter writer = new StreamWriter(FilePath);
            writer.WriteLine("; Generated by STSDEV at " + DateTime.Now.ToString());
            writer.WriteLine();
            writer.WriteLine(".OPTION EXPLICIT");
            writer.WriteLine(".Set CabinetNameTemplate=" + SolutionBuilder.SolutionName + ".wsp");
            writer.WriteLine(".set DiskDirectoryTemplate=CDROM");
            writer.WriteLine(".Set CompressionType=MSZIP");
            writer.WriteLine(".Set UniqueFiles=off");
            writer.WriteLine(".Set Cabinet=on");
            writer.WriteLine(".Set DiskDirectory1=\"" + outdir + "\"");
            writer.WriteLine();

            writer.WriteLine(";*** Solution manifest");
            writer.WriteLine(Globals.DeploymentFilesFolder + @"\manifest.xml");

            if (SolutionBuilder.Assemblies.Count > 0)
            {
                writer.WriteLine("");
                writer.WriteLine(";*** Assembly files");
                foreach (SolutionAssembly asm in SolutionBuilder.Assemblies)
                {
                    writer.WriteLine("\"" + outdir + asm.Location + "\"");
                }
            }

            string[] webpartFiles = Directory.GetFiles(Globals.RootFilesFolder, "*.webpart");
            if (webpartFiles.Length > 0)
            {
                writer.WriteLine("");
                writer.WriteLine(";*** Global webpart Files ***");
                foreach (string webpartFile in webpartFiles)
                    writer.WriteLine(webpartFile);
            }

            string[] dwpFiles = Directory.GetFiles(Globals.RootFilesFolder, "*.dwp");
            if (dwpFiles.Length > 0)
            {
                writer.WriteLine("");
                writer.WriteLine(";*** Global dwp Files ***");
                foreach (string dwpFile in dwpFiles)
                    writer.WriteLine(dwpFile);
            }
            

            if (Directory.Exists(Globals.FeaturesFolder))
            {
                WriteFeatureFiles(writer);
                writer.WriteLine();
            }

            if (Directory.Exists(Globals.TemplatesFolder))
            {
                // write TemplateFiles
                writer.WriteLine("");
                writer.WriteLine(";***********************************");
                writer.WriteLine(";*** Begin TemplateFiles section ***");
                writer.WriteLine(";***********************************");
                WriteTemplateFiles(writer);
                writer.WriteLine();
            }

            if (Directory.Exists(Globals.IsapiFolder) || Directory.Exists(Globals.ResourcesFolder))
            {
                // write RootFiles
                writer.WriteLine("");
                writer.WriteLine(";*******************************");
                writer.WriteLine(";*** Begin RootFiles section ***");
                writer.WriteLine(";*******************************");
                WriteRootFiles(writer);
                writer.WriteLine();
            }

            writer.Close();

        }

        static void WriteFeatureFiles(StreamWriter writer)
        {
            EnumerateFeatureDirectory(writer, Globals.FeaturesFolder);
        }

        static void EnumerateFeatureDirectory(StreamWriter writer, string path)
        {
            if ((Directory.GetFiles(path).Length > 0) &&
                 (path != Globals.FeaturesFolder))
            {
                // write files
                int PathOffset = (Globals.FeaturesFolder + @"\").Length;
                writer.WriteLine();
                writer.WriteLine(";*** add files for " + path.Substring(PathOffset) + " feature");
                writer.WriteLine(".Set DestinationDir=" + path.Substring(PathOffset));
                foreach (string file in Directory.GetFiles(path))
                {
                    writer.WriteLine(file);
                }

            }

            foreach (string folder in Directory.GetDirectories(path))
            {
                EnumerateFeatureDirectory(writer, folder);
            }
        }

        static void WriteTemplateFiles(StreamWriter writer)
        {
            EnumerateTemplateDirectory(writer, Globals.TemplatesFolder);
        }

        static void EnumerateTemplateDirectory(StreamWriter writer, string path)
        {
            // do nothing for folders in FEATURES folder
            if (path.Contains("FEATURES"))
            {
                return;
            }
            // write files
            if ((Directory.GetFiles(path).Length > 0) &&
                (path != Globals.TemplatesFolder))
            {
                int PathOffset = (Globals.TemplatesFolder + @"\").Length;
                writer.WriteLine("");
                writer.WriteLine(".Set DestinationDir=" + path.Substring(PathOffset));
                foreach (string file in Directory.GetFiles(path))
                {
                    writer.WriteLine(file);
                }
            }

            foreach (string folder in Directory.GetDirectories(path))
            {
                EnumerateTemplateDirectory(writer, folder);
            }
        }

        static void WriteRootFiles(StreamWriter writer)
        {
            EnumerateRootFilesDirectory(writer, Globals.RootFilesFolder);
        }

        static void EnumerateRootFilesDirectory(StreamWriter writer, string path)
        {
            // do nothing for folders in TEMPLATE folder
            if (path.Contains("TEMPLATE"))
            {
                return;
            }
            // write files
            if ((Directory.GetFiles(path).Length > 0) &&
                (path != Globals.RootFilesFolder))
            {
                int PathOffset = (Globals.RootFilesFolder + @"\").Length;
                writer.WriteLine("");
                writer.WriteLine(".Set DestinationDir=" + path.Substring(PathOffset));
            }
            foreach (string file in Directory.GetFiles(path))
            {
                writer.WriteLine(file);
            }

            foreach (string folder in Directory.GetDirectories(path))
            {
                EnumerateRootFilesDirectory(writer, folder);
            }
        }



        static void WriteAssembly(XmlWriter writer, string file, AssemblyDeploymentTarget target)
        {
            writer.WriteStartElement("Assembly");
            writer.WriteStartAttribute("Location");
            writer.WriteValue(file);
            writer.WriteEndAttribute();
            writer.WriteStartAttribute("DeploymentTarget");
            writer.WriteValue(target.ToString());
            writer.WriteEndAttribute();
            writer.WriteEndElement();
        }
    }
}
